package com.action.basic;

import java.io.IOException;
import java.util.List;

import com.action.common.Ajax;
import com.action.common.WEB;
import com.action.setting.JobMenuAction;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.basic.CUST;
import com.service.basic.ICUSTService;
import com.util.ActionLog;
import com.util.GTC;

public class CUSTAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private ICUSTService custService;
	private List<CUST> custs;
	private String arraydatas;
	
	//setter getter method
	
	public List<CUST> getCusts() {
		return custs;
	}

	public String getArraydatas() {
		return arraydatas;
	}

	public void setArraydatas(String arraydatas) {
		this.arraydatas = arraydatas;
	}

	public void setCusts(List<CUST> custs) {
		this.custs = custs;
	}
	
	public void setCustService(ICUSTService custService) {
		this.custService = custService;
	}

	//action method
	
	public String findAllCUST(){
		ActionLog.info(JobMenuAction.class,"["+WEB.getClassName(this.getClass())+"]:【加載所有客戶/廠商資料】");
		List<CUST> temps = custService.findAllCUST();
		this.setCusts(temps);
		this.allCustFormatExtArray(temps);
		return SUCCESS;
	}
	
	public void findAllCUSTByAjax(){
		ActionLog.info(this.getClass(),"["+WEB.getClassName(this.getClass())+"]:【Ajax加載所有客戶/廠商資料】");
		List<CUST> temps = custService.findAllCUST();
		this.allCustFormatExtArray(temps);
		try {
			Ajax.sendMsgAjax(this.getArraydatas());
		} catch (IOException e) {
			ActionLog.info(JobMenuAction.class,"["+WEB.getClassName(this.getClass())+"]:【Ajax加載所有客戶/廠商資料】异常:"+e.getMessage());
			e.printStackTrace();
		}
	}
	
	public void allCustFormatExtArray(List<CUST> lists){
		ActionLog.info(this.getClass(),"["+WEB.getClassName(this.getClass())+"]:【客戶/廠商對象轉換為JS字符串形式顯示在頁面】");
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(CUST cust : lists){
			sb.append("['"+cust.getCUS_NO()+"','"+GTC.nullEmpty(cust.getOBJ_ID())+"','"+GTC.nullEmpty(cust.getCUS_ARE())+"','"+GTC.nullEmpty(cust.getNAME())+"','"+GTC.nullEmpty(cust.getSNM())+"','"+GTC.nullEmpty(cust.getBOS_NM())+"','"+GTC.nullEmpty(cust.getCNT_MAN1())+"','"+GTC.nullEmpty(cust.getCNT_MAN2())+"','"+GTC.nullEmpty(cust.getACC_MAN())+"','"+GTC.nullEmpty(cust.getTEL1())+"','"+GTC.nullEmpty(cust.getTEL2())+"','"+GTC.nullEmpty(cust.getFAX())+"','"+GTC.nullEmpty(cust.getFAX())+"'],");
		}
		this.setArraydatas(sb.toString().substring(0, sb.toString().length()-1)+"]");
	}
}
